#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


def checkFlatFollowerRadius(radiusOfCurvatureList):
    """returns a boolean that its value is true if the
    profile is adecuate from a radius of curvature point of view
    and it also returns a text that warns about what problems
    the profile presents
    """
    adecuateProfile = False
    peak = False
    undercutting = False
    
    for radiusOfCurvature in radiusOfCurvatureList:
        if (radiusOfCurvature == 0):  # the outline has a peak
            peak = True
        elif (radiusOfCurvature < 0):
            undercutting = True
        else:
            pass

    if peak:
        message = _fromUtf8("The fenomena peak is present")
    elif undercutting:
        message = _fromUtf8("The fenomena undercutting cam profile is present")
    elif undercutting and peak:
        message = _fromUtf8("Both peak and undercutting cam profile are present")
    else:
        adecuateProfile = True
        message = _fromUtf8("Profile free from peaks and undercuttings")

    return adecuateProfile, message


def checkRollerFollowerRc(radiusOfCurvatureList, radiusOfCurvaturePList, rf):
    """returns a boolean that its value is true if the
    profile is adecuate from a radius of curvature point of view
    and it also returns a text that warns about what problems
    the profile presents
    """
    adecuateProfile = False
    peak = False
    noContact = False
    undercutting = False
    
    for radiusOfCurvature, radiusOfCurvatureP in zip(radiusOfCurvatureList,
                                                     radiusOfCurvaturePList):
        if (radiusOfCurvature < 0):
            if abs(radiusOfCurvature) <= rf:  # No contact
                noContact = True
            else:
                pass
        elif (radiusOfCurvature > 0):
            if (radiusOfCurvatureP == rf):  # There is a peak
                peak = True
            elif (radiusOfCurvatureP < rf):  # Rebaje
                undercutting = True
            else:
                pass

    if peak:
        message = _fromUtf8("The fenomena peak is present")
    elif undercutting:
        message = _fromUtf8("The fenomena undercutting cam profile is present")
    elif noContact:
        message = _fromUtf8("Zones out of reach for the roller")
    elif undercutting and peak and noContact:
        message = _fromUtf8("Peak, undercutting and zones out of reach are present")
    elif undercutting and peak and not noContact:
        message = _fromUtf8("Peak and undercutting are present")
    elif undercutting and not peak and noContact:
        message = _fromUtf8("Undercutting and zones out of reach are present")
    elif not undercutting and peak and noContact:
        message = _fromUtf8("Peak and zones out of reach are present")
    else:
        adecuateProfile = True
        message = _fromUtf8("Profile free from peaks, undercuttings and regions out of the reach of the follower's roller")

    return adecuateProfile, message
